import { Injectable } from '@angular/core';
import { ApolloQueryResult, FetchResult } from '@apollo/client/core';
import { Apollo, gql, QueryRef } from 'apollo-angular';
import { Observable } from 'rxjs';
import { NamesServiceApi,
NamesService_lookupResult,
NamesService_lookupVariables,
NamesService_dirResult,
NamesService_dirVariables,
NamesService_findAllFilesResult,
NamesService_findAllFilesVariables,
NamesService_findFilesResult,
NamesService_findFilesVariables,
NamesService_listResult,
NamesService_listVariables,
NamesService_totalCountResult,
NamesService_searchResult,
NamesService_searchVariables,
NamesService_loadAllResult,
NamesService_getByIdResult,
NamesService_getByIdVariables,
NamesService_mkdirResult,
NamesService_mkdirVariables,
NamesService_moveToRecycleBinResult,
NamesService_moveToRecycleBinVariables,
NamesService_removePathResult,
NamesService_removePathVariables,
NamesService_saveResult,
NamesService_saveVariables,
NamesService_removeByIdResult,
NamesService_removeByIdVariables,
NamesService_removeByIdListResult,
NamesService_removeByIdListVariables,
MelonNamesResultInfo, MelonNamesPageInfo,
MelonNamesConnection,
MelonNamesPackage,
MelonNamesOrderBy,
MelonNamesMutationInfo,
MelonNamesStringFilter,
MelonNamesNumberFilter,
MelonNamesBooleanFilter,
namesServiceQuery,
namesServiceMutation,
NamesServiceQueryOptions,
MelonNamesStore,
MelonNamesVoView,
MelonNamesInput,
MelonNamesFilter,
MelonNamesExtVo,
MelonNamesErrorBody} from '../model/names_service.model';

/************************************************************************
 * 名称服务
 */
@Injectable({
    providedIn: 'root'
})
export class NamesService implements NamesServiceApi {

        private lookupGql:string;
        private dirGql:string;
        private findAllFilesGql:string;
        private findFilesGql:string;
        private listGql:string;
        private totalCountGql:string;
        private searchGql:string;
        private searchObjectRef: QueryRef<NamesService_searchResult>;
        private loadAllGql:string;
        private getByIdGql:string;

    constructor(private apollo: Apollo) {
        this.lookupGql=`query nars_namesService_lookup($path: String!){${namesServiceQuery.namesService_lookup}}`;
        this.dirGql=`query nars_namesService_dir($path: String!,$first: Int!,$offset: Float!,$filter: MelonNamesFilter!,$orderBy: OrderBy){${namesServiceQuery.namesService_dir}}`;
        this.findAllFilesGql=`query nars_namesService_findAllFiles($path: String!){${namesServiceQuery.namesService_findAllFiles}}`;
        this.findFilesGql=`query nars_namesService_findFiles($path: String!,$first: Int!,$offset: Float!){${namesServiceQuery.namesService_findFiles}}`;
        this.listGql=`query nars_namesService_list($filter: MelonNamesFilter!,$orderBy: OrderBy){${namesServiceQuery.namesService_list}}`;
        this.totalCountGql=`query nars_namesService_totalCount{${namesServiceQuery.namesService_totalCount}}`;
        this.searchGql=`query nars_namesService_search($first: Int!,$offset: Float!,$filter: MelonNamesFilter!,$orderBy: OrderBy){${namesServiceQuery.namesService_search}}`;
        this.searchObjectRef = apollo.watchQuery<NamesService_searchResult>({ query: gql`${this.searchGql}`, fetchPolicy:'cache-first' });
        this.loadAllGql=`query nars_namesService_loadAll{${namesServiceQuery.namesService_loadAll}}`;
        this.getByIdGql=`query nars_namesService_getById($id: String!){${namesServiceQuery.namesService_getById}}`;
    }
    /**
     * 查找资源
     */
    lookup(variables: NamesService_lookupVariables, options:NamesServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<NamesService_lookupResult>> {
            return this.apollo.query({ query: gql`${this.lookupGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 查询目录
     */
    dir(variables: NamesService_dirVariables, options:NamesServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<NamesService_dirResult>> {
            return this.apollo.query({ query: gql`${this.dirGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 获取目录下所有文件
     */
    findAllFiles(variables: NamesService_findAllFilesVariables, options:NamesServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<NamesService_findAllFilesResult>> {
            return this.apollo.query({ query: gql`${this.findAllFilesGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 获取文件
     */
    findFiles(variables: NamesService_findFilesVariables, options:NamesServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<NamesService_findFilesResult>> {
            return this.apollo.query({ query: gql`${this.findFilesGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 分页查询
     */
    list(variables: NamesService_listVariables, options:NamesServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<NamesService_listResult>> {
            return this.apollo.query({ query: gql`${this.listGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 统计库中所有对象的数据量
     */
    totalCount(options:NamesServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<NamesService_totalCountResult>> {
            return this.apollo.query({ query: gql`${this.totalCountGql}`,fetchPolicy:options.cachePolicy});
    }
    /**
     * 游标查询
     */
    search(variables: NamesService_searchVariables, options:NamesServiceQueryOptions = { cachePolicy: 'cache-first', more: false }): Observable<ApolloQueryResult<NamesService_searchResult>> {
        this.searchObjectRef.setVariables(variables);
        if(options.more){
            this.searchObjectRef.fetchMore({variables});
        }
        return this.searchObjectRef.valueChanges;
    }
    /**
     * 获取库中所有数据，数据量大时请谨慎使用
     */
    loadAll(options:NamesServiceQueryOptions = { cachePolicy: 'cache-first', more: false }): Observable<ApolloQueryResult<NamesService_loadAllResult>> {
            return this.apollo.query({ query: gql`${this.loadAllGql}`,fetchPolicy:options.cachePolicy});
    }
    /**
     * 获取一个对象
     */
    getById(variables: NamesService_getByIdVariables, options:NamesServiceQueryOptions = { cachePolicy: 'cache-first', more: false }): Observable<ApolloQueryResult<NamesService_getByIdResult>> {
            return this.apollo.query({ query: gql`${this.getByIdGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 创建目录
     */
    mkdir(variables: NamesService_mkdirVariables): Observable<FetchResult<NamesService_mkdirResult>>{
        return this.apollo.mutate({mutation: gql`mutation nars_namesService_mkdir($path: String!){${namesServiceMutation.namesService_mkdir}}`, variables,fetchPolicy:'no-cache'});
    }
    /**
     * 移至回收站
     */
    moveToRecycleBin(variables: NamesService_moveToRecycleBinVariables): Observable<FetchResult<NamesService_moveToRecycleBinResult>>{
        return this.apollo.mutate({mutation: gql`mutation nars_namesService_moveToRecycleBin($path: [String]!){${namesServiceMutation.namesService_moveToRecycleBin}}`, variables,fetchPolicy:'no-cache'});
    }
    /**
     * 彻底删除资源
     */
    removePath(variables: NamesService_removePathVariables): Observable<FetchResult<NamesService_removePathResult>>{
        return this.apollo.mutate({mutation: gql`mutation nars_namesService_removePath($paths: [String]!){${namesServiceMutation.namesService_removePath}}`, variables,fetchPolicy:'no-cache'});
    }
    /**
     * 保存对象
     */
    save(variables: NamesService_saveVariables): Observable<FetchResult<NamesService_saveResult>>{
        return this.apollo.mutate({mutation: gql`mutation nars_namesService_save($entity: MelonNamesInput!){${namesServiceMutation.namesService_save}}`, variables,fetchPolicy:'no-cache'});
    }
    /**
     * 删除一个对象
     */
    removeById(variables: NamesService_removeByIdVariables): Observable<FetchResult<NamesService_removeByIdResult>>{
        return this.apollo.mutate({mutation: gql`mutation nars_namesService_removeById($id: String!){${namesServiceMutation.namesService_removeById}}`, variables,fetchPolicy:'no-cache'});
    }
    /**
     * 删除一批对象
     */
    removeByIdList(variables: NamesService_removeByIdListVariables): Observable<FetchResult<NamesService_removeByIdListResult>>{
        return this.apollo.mutate({mutation: gql`mutation nars_namesService_removeByIdList($ids: [String]!){${namesServiceMutation.namesService_removeByIdList}}`, variables,fetchPolicy:'no-cache'});
    }









}
